package me.stone.stanimeclient.core.data.domain.fan

import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import me.stone.stanimeclient.core.common.di.IoDispatcher
import me.stone.stanimeclient.core.data.domain.FlowUseCase
import me.stone.stanimeclient.core.data.domain.Result
import me.stone.stanimeclient.core.data.domain.data.LoadRankDataListRepository
import me.stone.stanimeclient.core.data.repository.RemoteFanDataRepository
import me.stone.stanimeclient.core.model.FanRankItem
import javax.inject.Inject
import javax.inject.Singleton

@Singleton
open class LoadRankDataCase @Inject constructor(
    private val repository: LoadRankDataListRepository,
    @IoDispatcher private val dispatcher: CoroutineDispatcher
) : FlowUseCase<Int?, List<List<FanRankItem>>>(dispatcher) {

    override fun execute(params: Int?): Flow<Result<List<List<FanRankItem>>>> {
        return flow {
            emit(Result.Loading)
            emit(Result.Success(repository.loadRankData(params).let { data ->
                listOf(data.weekly, data.monthly, data.overall)
            }))
        }
    }

}